<?php
require_once 'fun.php';
require_once 'push.php';
$param = file_get_contents('php://input');
// $param = file_get_contents('notify1.txt');
file_put_contents('notify.txt', $param);
$param = json_decode($param, true);
$msg = json_decode($param['msg'], true);

$timestamp = $param['timestamp'];
$nonce = $param['nonce'];
$type = $param['type'];
$appid = $msg['appid'];
$cp_orderno = $msg['cp_orderno'];
$cp_extra = $msg['cp_extra'];
$way = $msg['way'];
$channel_no = $msg['channel_no'];
$payment_order_no = $msg['payment_order_no'];
$total_amount = $msg['total_amount'];
$status = $msg['status'];
$item_id = $msg['item_id'];
$seller_uid = $msg['seller_uid'];
$paid_at = $msg['paid_at'];
$order_id = $msg['order_id'];
$url = file_get_contents('notify_url.txt');

// $resdata = send_post($url, $param);
file_put_contents('resdata.txt', $param);

if ($msg['status'] == 'SUCCESS') {
    $data = [
        $timestamp,
        $nonce,
        $type,
        $appid,
        $cp_orderno,
        $cp_extra,
        $way,
        $channel_no,
        $payment_order_no,
        $total_amount,
        $status,
        $item_id,
        $seller_uid,
        $paid_at,
        $order_id,
    ];
    function arrayToXml($arr)
    {

        $xml = "<xml>";
        foreach ($arr as $key => $val) {
            if (is_numeric($val)) {
                $xml .= "<$key>$val</$key>";
            } else
                $xml .= "<$key><![CDATA[$val]]></$key>";
        }
        $xml .= "</xml>";
        return $xml;
    }
    // echo arrayToXml($msg);exit;
    $res = send_post($url, arrayToXml($msg));
    $cp_extra = strToData($cp_extra);
    // var_dump($cp_extra);

    $cp = getPush([
        // "access_token" => getAccessToken(),
        "app_name" => "douyin",
        "open_id" => $cp_extra['open_id'],
        "order_detail" => json_encode([
            "order_id" => $cp_orderno,
            "create_time" => (int) $cp_extra['create_time'],  //订单创建时间
            "status" => "已支付",
            "amount" => 1,
            "total_price" => $total_amount,
            "detail_url" => $cp_extra['detail_url'],
            "item_list" => [
                [
                    "item_code" => $cp_extra['item_code'],
                    "img" => $cp_extra['img'],
                    "title" => $cp_extra['title'],
                    "sub_title" => $cp_extra['sub_title'], //子订单商品介绍副标题
                    "amount" => 1,
                    "price" => $total_amount

                ]
            ]
        ]),
        "order_status" => 1,
        "order_type" => 0,
        "update_time" => time(),

    ]);
    // var_dump($cp);
    echo json_encode(['err_no' => 0, 'err_tips' => 'success']);exit;
} else {
    send_post($url, [
        $timestamp,
        // $nonce,
        // $type,
        // $appid,
        // $cp_orderno,
        // $cp_extra,
        // $way,
        // $channel_no,
        // $payment_order_no,
        // $total_amount,
        // $status,
        // $item_id,
        // $seller_uid,
        // $paid_at,
        // $order_id,
    ]);
    echo json_encode(['err_no' => 400, 'err_tips' => 'business fail']);exit;
}

function send_post($url, $post_data)
{
    // $postdata = http_build_query($post_data);
    file_put_contents('xml.txt', $post_data);
    $options = array(
        'http' => array(
            'method' => 'POST',
            'header' => 'Content-type:application/xml',
            'content' => $post_data,
            'timeout' => 15 * 60 // 超时时间（单位:s）
        )
    );
    $context = stream_context_create($options);
    $result = file_get_contents($url, false, $context);
    return $result;
}
